﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ShopSpace.Core
{
	public abstract class Entity : IEquatable<Entity>
	{
		public virtual int Id
		{
			get;
			private set;
		}

		public virtual DateTime ConcurrencyDate
		{
			get;
			set;
		}

		public virtual DateTime AddedDate
		{
			get;
			private set;
		}

		public virtual string AddedBy
		{
			get;
			private set;
		}

		public virtual Nullable<DateTime> UpdatedDate
		{
			get;
			private set;
		}

		public virtual string UpdatedBy
		{
			get;
			private set;
		}

		public virtual void AuditAdd(string addedBy)
		{
			this.AddedBy = addedBy;
			this.AddedDate = DateTime.Now;
		}

		public virtual void AuditUpdate(string updatedBy)
		{
			this.UpdatedBy = updatedBy;
			this.UpdatedDate = DateTime.Now;
		}

		public virtual bool Equals(Entity obj)
		{
			if (ReferenceEquals(null, obj))
				return false;
			if (ReferenceEquals(this, obj))
				return true;
			if (GetType() != obj.GetType())
				return false;
			return obj.Id == Id;
		}

		public override bool Equals(object obj)
		{
			if (ReferenceEquals(null, obj))
				return false;
			if (ReferenceEquals(this, obj))
				return true;
			if (GetType() != obj.GetType())
				return false;
			return Equals((Entity)obj);
		}

		public override int GetHashCode()
		{
			return (Id.GetHashCode() * 397) ^ GetType().GetHashCode();
		}

		public static bool operator ==(Entity left, Entity right)
		{
			return Equals(left, right);
		}

		public static bool operator !=(Entity left, Entity right)
		{
			return !Equals(left, right);
		}
	}
}
